//
//  NMMapSeachViewController.m
//  NotificationMe
//
//  Created by max on 2018/12/16.
//  Copyright © 2018年 dayang. All rights reserved.
//

#import "NMMapSeachViewController.h"

@interface NMMapSeachViewController ()<UIGestureRecognizerDelegate, MKMapViewDelegate>

@property (nonatomic, strong) MKMapView *mapView;

@property (nonatomic, strong) MKPointAnnotation *pointAnnotation;

@property (nonatomic, strong) UIButton *doneButton;

@end

@implementation NMMapSeachViewController

- (instancetype)initWithMap:(MKMapView *)mapView {
    if (self = [super init]) {
        _mapView = mapView;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViews];
}

- (void)setupViews {
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBarItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backBarItem;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    self.title = @"Map";
    
    [self.view addSubview:self.mapView];
    self.mapView.delegate = self;
    
    [_mapView setUserTrackingMode:MKUserTrackingModeFollow];
    [_mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake([NMCLLocationManager shared].location.coordinate.latitude, [NMCLLocationManager shared].location.coordinate.longitude), MKCoordinateSpanMake(0.05, 0.05));
    [_mapView setRegion:region animated:false];
    
    UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
    [self.mapView addGestureRecognizer:mTap];
    
    
    [self.view addSubview:self.doneButton];
    [_doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(-22);
        make.right.equalTo(self.view.mas_right).offset(-44);
        make.left.equalTo(self.view.mas_left).offset(44);
        make.height.mas_equalTo(44);
    }];
    
    if ([NMCLLocationManager shared].selectionCllocation.latitude) {
        _mapView.centerCoordinate = [NMCLLocationManager shared].selectionCllocation;
        self.pointAnnotation.coordinate = [NMCLLocationManager shared].selectionCllocation;
        [self.mapView addAnnotation:self.pointAnnotation];
    }
}

- (void)backButtonClicked:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapPress:(UIGestureRecognizer *)gestureRecognizer {
    
    CGPoint point = [gestureRecognizer locationInView:self.mapView];
    CLLocationCoordinate2D touchMapCoordinate = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
    self.pointAnnotation.coordinate = touchMapCoordinate;
    [self.mapView addAnnotation:self.pointAnnotation];
}

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id)annotation {
    MKPinAnnotationView *pinView = nil;
    if(annotation != self.mapView.userLocation) {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if (pinView == nil) {
            pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID];
            pinView.animatesDrop = false;
            pinView.canShowCallout = false;
        }
    }
    return pinView;
}

- (void)doneButtonClicked {
    [NMCLLocationManager shared].selectionCllocation = self.pointAnnotation.coordinate;
    [self.navigationController popViewControllerAnimated:true];
}

- (MKPointAnnotation *)pointAnnotation {
    if (!_pointAnnotation) {
        _pointAnnotation = [[MKPointAnnotation alloc] init];
        
    }
    return _pointAnnotation;
}

- (MKMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
    }
    return _mapView;
}

- (UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton new];
        _doneButton.backgroundColor = UIColor.FFBlue;
        _doneButton.titleLabel.font = [UIFont boldSystemFontOfSize:16.0];
        [_doneButton setTitle:@"Confirm selection" forState:UIControlStateNormal];
        [_doneButton addTarget:self action:@selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        _doneButton.layer.cornerRadius = 22;
        _doneButton.layer.masksToBounds = YES;
    }
    return _doneButton;
}

@end



























